<template>
  <div class="reset-password-container">
    <div class="reset-password-card">
      <h2 class="reset-password-title">重置密码</h2>
      
      <van-form @submit="onSubmit">
        <van-field
          v-model="form.oldPassword"
          type="password"
          name="oldPassword"
          label="旧密码"
          placeholder="请输入旧密码"
          :rules="[{ required: true, message: '请输入旧密码' }]"
        />
        <van-field
          v-model="form.newPassword"
          type="password"
          name="newPassword"
          label="新密码"
          placeholder="请输入新密码"
          :rules="[
            { required: true, message: '请输入新密码' },
            { validator: validatePassword, message: '密码长度不能少于6位' }
          ]"
        />
        <van-field
          v-model="form.confirmPassword"
          type="password"
          name="confirmPassword"
          label="确认密码"
          placeholder="请再次输入新密码"
          :rules="[
            { required: true, message: '请确认新密码' },
            { validator: validateConfirmPassword, message: '两次输入的密码不一致' }
          ]"
        />
        
        <div style="margin: 24px 16px;">
          <van-button round block type="primary" native-type="submit" :loading="submitting">
            确认修改
          </van-button>
        </div>
      </van-form>
      
      <div class="back-link">
        <span @click="goBack">返回首页</span>
      </div>
    </div>
  </div>
</template>

<script setup>
import { ref } from 'vue'
import { useRouter } from 'vue-router'
import { Form, Field, Button, showSuccessToast, showFailToast } from 'vant'
import { resetPassword } from '@/api'

const router = useRouter()
const submitting = ref(false)
const form = ref({
  oldPassword: '',
  newPassword: '',
  confirmPassword: ''
})

function validatePassword(val) {
  return val.length >= 6
}

function validateConfirmPassword(val) {
  return val === form.value.newPassword
}

async function onSubmit() {
  if (form.value.newPassword !== form.value.confirmPassword) {
    showFailToast('两次输入的密码不一致')
    return
  }
  
  submitting.value = true
  
  try {
    await resetPassword(form.value.oldPassword, form.value.newPassword)
    showSuccessToast('密码修改成功')
    
    // 清空表单
    form.value = {
      oldPassword: '',
      newPassword: '',
      confirmPassword: ''
    }
    
    // 延迟跳转到首页
    setTimeout(() => {
      router.push('/')
    }, 1500)
  } catch (error) {
    console.error('密码修改失败:', error)
    const message = error.response?.data?.error || '服务器错误'
    showFailToast(message)
  } finally {
    submitting.value = false
  }
}

function goBack() {
  router.push('/')
}
</script>

<style scoped>
.reset-password-container {
  min-height: 100vh;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  background: #f6f7fb;
  padding: 60px 20px 20px 20px;
}

.reset-password-card {
  width: 100%;
  max-width: 400px;
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 2px 12px 0 rgba(0, 0, 0, 0.05);
  overflow: hidden;
  padding: 24px 16px;
}

.reset-password-title {
  text-align: center;
  margin-bottom: 24px;
  color: #333;
  font-size: 24px;
}

.back-link {
  text-align: center;
  margin-top: 16px;
  color: #27c19e;
  font-size: 14px;
}

.back-link span {
  cursor: pointer;
}
</style> 